package com.dbms.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dbms.domain.entity.BisXajmodeParas;
import com.dbms.domain.entity.BisXnjmodeProject;
import com.dbms.enums.ProjectEnum;
import com.dbms.service.BisXajmodeParasService;
import com.dbms.service.BisXnjmodeProjectService;
import com.dbms.service.CallbackService;
import com.dbms.utils.DateIntervalCalculator;
import com.dbms.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
@RequiredArgsConstructor
public class CallbackServiceImpl implements CallbackService {

    final BisXnjmodeProjectService bisXnjmodeProjectService;
    final BisXajmodeParasService bisXajmodeParasService;

    @Override
    public Boolean callback(JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        BisXnjmodeProject project = bisXnjmodeProjectService.querById(id);
        project.setResult(jsonObject.toString());
        project.setStatus(ProjectEnum.运行完成.getCode());
        project.setRendTime(new Date());
        // 率定模式单独处理
        if(jsonObject.getString("model").equals("1")){
            JSONObject xajpara = jsonObject.getJSONObject("xajpara");
            //更新bis_xajmode_paras数据
            BisXajmodeParas paras = bisXajmodeParasService.getInfoByStationCode2(project.getStcdR());
            //
            JSONObject cltw = xajpara.getJSONObject("cltw");
            paras.setCltw(Integer.valueOf(cltw.getString("rate")));
            paras.setCltwM(Integer.valueOf(cltw.getString("max")));
            paras.setCltwS(Integer.valueOf(cltw.getString("min")));
            //
            JSONObject ctw = xajpara.getJSONObject("ctw");
            paras.setCtw(Integer.valueOf(ctw.getString("rate")));
            paras.setCtwM(Integer.valueOf(ctw.getString("max")));
            paras.setCtwS(Integer.valueOf(ctw.getString("min")));
            //
            JSONObject dcsc = xajpara.getJSONObject("dcsc");
            paras.setDcsc(new BigDecimal(dcsc.getString("rate")));
            paras.setDcscM(new BigDecimal(dcsc.getString("max")));
            paras.setDcscS(new BigDecimal(dcsc.getString("min")));
            //
            JSONObject dec = xajpara.getJSONObject("dec");
            paras.setDec(new BigDecimal(dec.getString("rate")));
            paras.setDecM(new BigDecimal(dec.getString("max")));
            paras.setDecS(new BigDecimal(dec.getString("min")));
            //
            JSONObject fcdc = xajpara.getJSONObject("fcdc");
            paras.setFcdc(new BigDecimal(fcdc.getString("rate")));
            paras.setFcdcM(new BigDecimal(fcdc.getString("max")));
            paras.setFcdcS(new BigDecimal(fcdc.getString("min")));
            //
            JSONObject ogf = xajpara.getJSONObject("ogf");
            paras.setOgf(new BigDecimal(ogf.getString("rate")));
            paras.setOgfM(new BigDecimal(ogf.getString("max")));
            paras.setOgfS(new BigDecimal(ogf.getString("min")));
            //
            JSONObject pia = xajpara.getJSONObject("pia");
            paras.setPia(new BigDecimal(pia.getString("rate")));
            paras.setPiaM(new BigDecimal(pia.getString("max")));
            paras.setPiaS(new BigDecimal(pia.getString("min")));
            //
            JSONObject rcfr = xajpara.getJSONObject("rcfr");
            paras.setRcfr(new BigDecimal(rcfr.getString("rate")));
            paras.setRcfrM(new BigDecimal(rcfr.getString("max")));
            paras.setRcfrS(new BigDecimal(rcfr.getString("min")));
            //
            JSONObject uctw = xajpara.getJSONObject("uctw");
            paras.setUctw(Integer.valueOf(uctw.getString("rate")));
            paras.setUctwM(Integer.valueOf(uctw.getString("max")));
            paras.setUctwS(Integer.valueOf(uctw.getString("min")));

            //
            JSONObject ugo = xajpara.getJSONObject("ugo");
            paras.setUctw(Integer.valueOf(ugo.getString("rate")));
            paras.setUctwM(Integer.valueOf(ugo.getString("max")));
            paras.setUctwS(Integer.valueOf(ugo.getString("min")));

            //
            JSONObject zsfxxs = xajpara.getJSONObject("zsfxxs");
            paras.setZsfxxs(new BigDecimal(zsfxxs.getString("rate")));
            paras.setZsfxxsM(new BigDecimal(zsfxxs.getString("max")));
            paras.setZsfxxsS(new BigDecimal(zsfxxs.getString("min")));

            //
            JSONObject zysrl = xajpara.getJSONObject("zysrl");
            paras.setZysrl(Integer.valueOf(zysrl.getString("rate")));
            paras.setZysrlM(Integer.valueOf(zysrl.getString("max")));
            paras.setZysrlS(Integer.valueOf(zysrl.getString("min")));

            bisXajmodeParasService.update(paras);

        }
        //
        JSONArray array = jsonObject.getJSONArray("result");
        JSONObject resultObject = JsonUtil.findMaxMqObject(array);
        project.setMaxQ(new BigDecimal(resultObject.getString("mq")));
        project.setMaxQTime(DateIntervalCalculator.stringToDate(resultObject.getString("time")));
        project.setStctRJson(array.toString());
        bisXnjmodeProjectService.update(project);
        return Boolean.TRUE;
    }
}
